import {getCookies, setCookies, removeCookies} from "util/auth";

const tools = {
    isPositiveInt:function(s){
        var re = /(^[1-9]\d*$)/;
        return re.test(s)
    },
    pushNode:function($this){
        const _this = this;
        // debugger
        const name = !$this.attr('data-name')?$this.html():$this.attr('data-name'),
            degree = parseInt($this.attr('data-type')),
            prevNode = $this.attr('data-parent'),
            grandfather = $this.attr('data-grandfather'),
            prevId = $this.attr('data-id');
        let breadMenu = getCookies('bmenu');
        let isExit = false;
        if(degree === 1) {
            if (!!breadMenu) {
                breadMenu = JSON.parse(breadMenu);
                breadMenu.splice(0, breadMenu.length);
            } else {
                breadMenu = [];
            }
            breadMenu.push({name, type: degree});
            breadMenu = JSON.stringify(breadMenu);
            setCookies('bmenu', breadMenu);
            return false;
        }
        if (!!breadMenu && breadMenu!=='[]') {
            breadMenu = JSON.parse(breadMenu);
            if (breadMenu.length===1 && degree ===2) {
                if (prevNode === breadMenu[0].name) {
                    breadMenu = JSON.stringify(breadMenu);
                    setCookies('bmenu', breadMenu);
                } else {
                    _this.doSecond(name, degree, prevNode, grandfather, prevId);
                    return false;
                }
                return false;
            }
            for (let i = 0; i < breadMenu.length; i++) {
                const delNum = breadMenu.length - i;
                if (breadMenu[i].type === degree) {
                    breadMenu.splice(i, delNum);
                    isExit = true;
                    breadMenu.push({name, type: degree, parent: prevNode, prevId});
                    break;
                }
                if (breadMenu[i].type === 1 && breadMenu[i].name !== grandfather && !!grandfather) {
                    _this.doSecond(name, degree, prevNode, grandfather, prevId);
                    return false;
                }
                if (breadMenu[i].type === 2 && breadMenu[i].name !== grandfather && !!grandfather) {
                    breadMenu[i].name = grandfather;
                }
            }
            if (!isExit && !!degree) {
                breadMenu.push({name, type: degree, parent: prevNode, prevId: prevId});
            }
            breadMenu = JSON.stringify(breadMenu);
            setCookies('bmenu', breadMenu);
        } else{
            _this.doSecond(name, degree, prevNode, grandfather, prevId);
        }
    },
    doSecond(name, degree, prevNode, grandfather, prevId) {
        let breadMenu = [];
        if (degree === 2) {
            breadMenu.push({name: name, type: degree, parent: prevNode, prevId: prevId});
        } else {
            breadMenu.push({name: grandfather, type: 2, parent: 'allProduct'});
            breadMenu.push({name, type: degree, parent: prevNode, prevId: prevId});
        }
        breadMenu = JSON.stringify(breadMenu);
        setCookies('bmenu', breadMenu);
    },
    saveResource($this) {
        const name = $this.attr('data-name'), model = $this.attr('data-model'), unit = $this.attr('data-unit'), id = $this.attr('data-id'), img = $this.attr('data-url');
        let cartList = localStorage.getItem('cartList'), dataCart = {}, isExit = false;
        if(!!cartList && cartList !== '[]') {
            cartList = JSON.parse(cartList);
            for (let i = 0; i < cartList.length; i++) {
                if (id === cartList[i].id) {
                    cartList[i].num++;
                    isExit = true;
                }
            }
            if(!isExit) {
                cartList.push({id, name, model, unit, img, num: 1});
            }
        } else {
            cartList = [];
            cartList.push({id, name, model, unit, img, num: 1});
        }
        console.log(cartList);
        localStorage.setItem('cartList', JSON.stringify(cartList));
    },
    msgShakeLayer:function(msg,count,skin,anim){
        var time = count?count:1000;
        var anim = anim?anim:5;
        if (skin) {
            layer.msg(msg,{time:time,skin:skin,anim:anim});
        }else{
            layer.msg(msg,{time:time,anim:anim});
        }
    }
};
module.exports = tools;